import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { defineAsyncComponent } from 'vue';

const routerHistory = createWebHistory(process.env.BASE_URL);

const publicRoutes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/ge_phonelogin'
  },
  {
  path: '/im1',
  component: () => import('../views/IM/T_im.vue')
  },
  {
    path: '/aouth_login',
    name: 'AuthLogin',
    component: defineAsyncComponent(() => import('../views/login/aouth_login.vue')),
  },
  {
    path: '/ge_phonelogin',
    name: 'PhoneLogin',
    component: defineAsyncComponent(() => import('../views/login/ge_phonelogin.vue')),
  },
  {
    path: '/ge_registration',
    name: 'Registration',
    component: defineAsyncComponent(() => import('../views/login/ge_registration.vue')),
  },
      {
    path: '/home',
    name: 'Home',
    component:  () => import('../views/Ho-me.vue'),
    redirect: '/users',
    meta: { requiresAuth: true },
    children: [
      {
        path: '/users',
        name: 'Users',
        component: () => import('../views/manager/us-er.vue'),
      },
      {
    path: '/im',
    component: () => import('../views/IM/link_home.vue')//使用了iform内嵌了T_im.vue页面,所以需要用到一个link页面
      },
      {
    path: '/update_down',
    component: () => import('../views/gongneng/update-down-pdf.vue')//使用了iform内嵌了T_im.vue页面,所以需要用到一个link页面
      },
      {
    path: '/update_user',
    component: () => import('../views/gongneng/update-user.vue')//使用了iform内嵌了T_im.vue页面,所以需要用到一个link页面
      },
      {
    path: '/tu_biao',
    component: () => import('../views/gongneng/tu-biao.vue')//使用了iform内嵌了T_im.vue页面,所以需要用到一个link页面
      },
    ]
  },
];

// 配置路由
const router = createRouter({
  history: routerHistory,
  routes: publicRoutes,
});
const path = localStorage.getItem('path')
if (path) {
  const jsonData = JSON.parse(path)
  jsonData.forEach((data:any) => {
  const childRoute: RouteRecordRaw = {
    path: data.path,
    name: data.name,
    component: defineAsyncComponent(() => import(`../views/${data.component}.vue`)),
  };
  router.addRoute('Home', childRoute);
});
}

export default router;
